package cn.uil.demo;

import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;

@Component
public class AsyncInitCoordinator implements SmartLifecycle {

    private final AsyncPostConstructProcessor processor;
    private volatile boolean running = false;

    public AsyncInitCoordinator(AsyncPostConstructProcessor processor) {
        this.processor = processor;
    }

    @Override
    public void start() {
        if (!running) {
            running = true;
            processor.awaitAsyncInitialization();
        }
    }

    @Override
    public void stop() {
        running = false;
    }

    @Override
    public boolean isRunning() {
        return running;
    }

    // 最高优先级，最后执行
    @Override
    public int getPhase() {
        return Integer.MAX_VALUE;
    }
}
